package com.usian.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.usian.pojo.Commodity;
import com.usian.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    CommodityService commodityService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Commodity> CList = commodityService.findAll();
        model.addAttribute("CList",CList);
        return "list";
    }

    @RequestMapping("/findAllAnd")
    public String findAllAnd(Model model){
        List<Commodity> CList = commodityService.findAllAnd();
        model.addAttribute("CList",CList);
        return "list";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }

    //跳转修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer cid, Model model) {
//   查询要修改商品信息
        Commodity c = commodityService.findById(cid);
        model.addAttribute("c", c);
        return "update";
    }

    @RequestMapping("/sava")
    public String sava(Commodity commodity, Model model) {
        if (commodity.getCstart() == 1) {
            if (commodity.getCid() == null) {
                commodityService.saveUser(commodity);
            } else {
                commodityService.updateUser(commodity);
            }

            List<Commodity> CList = commodityService.findAll();
            model.addAttribute("CList", CList);
            return "list";
        }
        return "没有权限";
    }


//    删除
    @RequestMapping("/del")
    public String del(Integer cid ,Model model) {
        Commodity commodity = commodityService.findById(cid);
        if (commodity.getCstart() == 1) {
            commodityService.del(cid);
            List<Commodity> CList = commodityService.findAll();
            model.addAttribute("CList", CList);
            return "redirect:/commodity/findAll";
        } else {
            return "没有权限删除";
        }
    }


    //提交审核
    @RequestMapping("/check")
    public String check(Integer cid, Model model) {
        Commodity commodity = commodityService.findById(cid);
        if (commodity.getCstart() == 1) {
            commodityService.check(cid);
            List<Commodity> commodityList = commodityService.findAll();
            model.addAttribute("commodityList", commodityList);
            return "redirect:/commodity/findAll";
        } else {
            return "已审核";
        }
    }


    @RequestMapping("/shangjia")
    public String shangjia(Integer cid){
        Commodity commodity = commodityService.findById(cid);
        if (commodity.getCstart()>3){

            commodityService.shangjia(cid);
        }
        return "redirect:/commodity/findAllAnd";
    }

    @RequestMapping("/xiajia")
    public String xiajia(Integer cid){
        Commodity commodity = commodityService.findById(cid);
        if (commodity.getCstart()==6){
            commodityService.xiajia(cid);
        }
        return "redirect:/commodity/findAllAnd";
    }

}
